package com.egao.orderB.biz.impl;

import com.egao.common.core.damon.constant.UserConstant;
import com.egao.common.core.damon.results.BaseDao;
import com.egao.common.core.damon.results.PageResult2;

import com.egao.common.core.damon.utils.DateUtil;
import com.egao.common.core.damon.utils.Utils;
import com.egao.common.system.entity.User;
import com.egao.orderB.biz.OceanPreConfigurationBiz;
import com.egao.orderB.entity.OceanPreConfiguration;
import com.egao.common.core.web.JsonResult;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.Map;
import java.util.List;

/**
 *
 * 数据层实现类
 * @author : Doman
 * @date : 2021-10-20 16:59:51
 * @version ：1.0
 */
@Repository
public class OceanPreConfigurationBizImpl extends BaseDao implements OceanPreConfigurationBiz {

	@Override
	public JsonResult list(PageResult2<T> page, OceanPreConfiguration oceanPreConfiguration, User loginUser) throws Exception {
		Map<String, String> map  = new HashMap<String, String>();
		RowMapper<OceanPreConfiguration> rm = new BeanPropertyRowMapper<OceanPreConfiguration>(OceanPreConfiguration.class);
		StringBuffer sql = new StringBuffer("select o.id,o.organization_id,o.booking_agent_id,o.booking_agent");
    sql.append(",o.ship,o.customer_id,o.customer,o.box_type");
    sql.append(",o.cus_plan_deposit_path,o.pre_storage_deposit_path");
    sql.append(",o.vessel,o.voyage,o.air_route,o.go_ship_day,o.entrustment_annex,o.cus_plan_deposit,o.pre_storage_deposit");
		sql.append(",o.cus_plan_price,o.pre_storage_price,o.pre_date,o.create_time,o.create_user");
		sql.append(",o.update_time,o.update_user");
    sql.append(",sogzn.organization_name organizationName,suser.nick_name createName ");
    sql.append(" from chf_ocean_pre_configuration o ");
    sql.append(" inner join sys_user suser on suser.user_id=o.create_user ");
    sql.append(" inner join sys_organization sogzn on sogzn.organization_id=o.organization_id and sogzn.deleted=0 ");
    sql.append(" where o.deleted=0 ");
		if(loginUser.getUserId() != UserConstant.ADMIN_ID){
      sql.append(" and o.organization_id=:tableOrganizationId ");
      map.put("tableOrganizationId", loginUser.getOrganizationId() + "");
    }
    if(Utils.isNotEmpty(oceanPreConfiguration)){
//			if(Utils.isNotEmpty(oceanPreConfiguration.getName())){
//				sql.append(" and instr(o.name,:name)>0 ");
//				map.put("name", oceanPreConfiguration.getName());
//			}
      if(Utils.isNotEmpty(oceanPreConfiguration.getSort())&&Utils.isNotEmpty(oceanPreConfiguration.getOrder())){
        sql.append(" order by o."+oceanPreConfiguration.getSort()+" "+oceanPreConfiguration.getOrder());
      } else {
        sql.append(" order by o.create_time desc");
      }
		}
		return jsonResult(getPageQueryList(page, sql.toString(), map, rm));
	}

	@Override
	public List<OceanPreConfiguration> list(OceanPreConfiguration oceanPreConfiguration, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		RowMapper<OceanPreConfiguration> rm = new BeanPropertyRowMapper<OceanPreConfiguration>(OceanPreConfiguration.class);
    StringBuffer sql = new StringBuffer("select o.id,o.organization_id,o.booking_agent_id,o.booking_agent");
    sql.append(",o.ship,o.customer_id,o.customer,o.box_type");
		sql.append(",o.cus_plan_deposit_path,o.pre_storage_deposit_path");
		sql.append(",o.vessel,o.voyage,o.air_route,o.go_ship_day,o.entrustment_annex,o.cus_plan_deposit,o.pre_storage_deposit");
		sql.append(",o.cus_plan_price,o.pre_storage_price,o.pre_date,o.create_time,o.create_user");
		sql.append(",o.update_time,o.update_user");
    sql.append(",sogzn.organization_name organizationName,suser.nick_name createName ");
    sql.append(" from chf_ocean_pre_configuration o ");
    sql.append(" inner join sys_user suser on suser.user_id=o.create_user ");
    sql.append(" inner join sys_organization sogzn on sogzn.organization_id=o.organization_id and sogzn.deleted=0 ");
    sql.append(" where o.deleted=0 ");
		if(loginUser.getUserId() != UserConstant.ADMIN_ID){
      sql.append(" and o.organization_id=:tableOrganizationId ");
      map.put("tableOrganizationId", loginUser.getOrganizationId() + "");
    }
    if(Utils.isNotEmpty(oceanPreConfiguration)){
      if(Utils.isNotEmpty(oceanPreConfiguration.getSort())&&Utils.isNotEmpty(oceanPreConfiguration.getOrder())){
        sql.append(" order by o."+oceanPreConfiguration.getSort()+" "+oceanPreConfiguration.getOrder());
      } else {
        sql.append(" order by o.create_time desc");
      }
    }
		getMapParameter(sql, map);
		return jdbcTemplate.query(sql.toString(),map, rm);
	}

	@Override
	public OceanPreConfiguration getById(OceanPreConfiguration oceanPreConfiguration) {
		RowMapper<OceanPreConfiguration> rm = new BeanPropertyRowMapper<OceanPreConfiguration>(OceanPreConfiguration.class);
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql = new StringBuffer("select o.id,o.organization_id,o.booking_agent_id,o.booking_agent");
    sql.append(",o.ship,o.customer_id,o.customer,o.box_type");
		sql.append(",o.cus_plan_deposit_path,o.pre_storage_deposit_path");
		sql.append(",o.vessel,o.voyage,o.air_route,o.go_ship_day,o.entrustment_annex,o.cus_plan_deposit,o.pre_storage_deposit");
		sql.append(",o.cus_plan_price,o.pre_storage_price,o.pre_date,o.create_time,o.create_user");
		sql.append(",o.update_time,o.update_user");
    sql.append(",sogzn.organization_name organizationName,suser.nick_name createName ");
    sql.append(" from chf_ocean_pre_configuration o ");
    sql.append(" inner join sys_user suser on suser.user_id=o.create_user ");
    sql.append(" inner join sys_organization sogzn on sogzn.organization_id=o.organization_id and sogzn.deleted=0 ");
    sql.append(" where o.deleted=0 and o.id=:id ");
		map.put("id", oceanPreConfiguration.getId());
		getMapParameter(sql, map);
		return jdbcTemplate.queryForObject(sql.toString(), map, rm);
	}

	@Override
	public boolean save(OceanPreConfiguration oceanPreConfiguration, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql =new StringBuffer("insert into chf_ocean_pre_configuration (");
    sql.append("organization_id,booking_agent_id,booking_agent,ship,customer_id");
		sql.append(",vessel,voyage,air_route,go_ship_day,entrustment_annex,cus_plan_deposit,pre_storage_deposit");
		sql.append(",cus_plan_deposit_path,pre_storage_deposit_path");
		sql.append(",customer,box_type,cus_plan_price,pre_storage_price,pre_date");
		sql.append(",create_user,create_time) values ( ");
    sql.append(":organization_id,:booking_agent_id,:booking_agent,:ship,:customer_id");
		sql.append(",:vessel,:voyage,:air_route,:go_ship_day,:entrustment_annex,:cus_plan_deposit,:pre_storage_deposit");
		sql.append(",:cus_plan_deposit_path,:pre_storage_deposit_path");
		sql.append(",:customer,:box_type,:cus_plan_price,:pre_storage_price,now()");
		sql.append(",:create_user,now())");
    map.put("organization_id", loginUser.getOrganizationId() + "");
    map.put("booking_agent_id", oceanPreConfiguration.getBookingAgentId()==null?null:oceanPreConfiguration.getBookingAgentId()+"");
    map.put("booking_agent", oceanPreConfiguration.getBookingAgent());
    map.put("ship", oceanPreConfiguration.getShip());
    map.put("vessel", oceanPreConfiguration.getVessel());
    map.put("voyage", oceanPreConfiguration.getVoyage());
    map.put("air_route", oceanPreConfiguration.getAirRoute());
    map.put("go_ship_day", oceanPreConfiguration.getGoShipDay());
    map.put("entrustment_annex", oceanPreConfiguration.getEntrustmentAnnex());
    map.put("cus_plan_deposit", oceanPreConfiguration.getCusPlanDeposit());
    map.put("pre_storage_deposit", oceanPreConfiguration.getPreStorageDeposit());
    map.put("cus_plan_deposit_path", oceanPreConfiguration.getCusPlanDepositPath());
    map.put("pre_storage_deposit_path", oceanPreConfiguration.getPreStorageDepositPath());
    map.put("customer_id", oceanPreConfiguration.getCustomerId()==null?null:oceanPreConfiguration.getCustomerId()+"");
    map.put("customer", oceanPreConfiguration.getCustomer());
    map.put("box_type", oceanPreConfiguration.getBoxType());
    map.put("cus_plan_price", oceanPreConfiguration.getCusPlanPrice());
    map.put("pre_storage_price", oceanPreConfiguration.getPreStoragePrice());
//    map.put("pre_date", oceanPreConfiguration.getPreDate());
		map.put("create_user", loginUser.getUserId() + "");
		getMapParameter(sql, map);
		int count = jdbcTemplate.update(sql.toString(), map);
		return count > 0;
	}

	@Override
	public boolean update(OceanPreConfiguration oceanPreConfiguration, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql =new StringBuffer("update chf_ocean_pre_configuration set ");
    sql.append("booking_agent_id=:booking_agent_id,booking_agent=:booking_agent,ship=:ship,customer_id=:customer_id ");
		sql.append(",customer=:customer,box_type=:box_type,cus_plan_price=:cus_plan_price,pre_storage_price=:pre_storage_price ");
		sql.append(",cus_plan_deposit_path=:cus_plan_deposit_path,pre_storage_deposit_path=:pre_storage_deposit_path");
		sql.append(",vessel=:vessel,voyage=:voyage,air_route=:air_route,entrustment_annex=:entrustment_annex,go_ship_day=:go_ship_day,cus_plan_deposit=:cus_plan_deposit,pre_storage_deposit=:pre_storage_deposit");
		sql.append(",pre_date=:pre_date,update_time=now(),update_user=:update_user where id=:id ");
    map.put("booking_agent_id", oceanPreConfiguration.getBookingAgentId()==null?null:oceanPreConfiguration.getBookingAgentId()+"");
    map.put("booking_agent", oceanPreConfiguration.getBookingAgent());
    map.put("ship", oceanPreConfiguration.getShip());
		map.put("vessel", oceanPreConfiguration.getVessel());
		map.put("voyage", oceanPreConfiguration.getVoyage());
		map.put("air_route", oceanPreConfiguration.getAirRoute());
		map.put("go_ship_day", oceanPreConfiguration.getGoShipDay());
		map.put("entrustment_annex", oceanPreConfiguration.getEntrustmentAnnex());
		map.put("cus_plan_deposit", oceanPreConfiguration.getCusPlanDeposit());
		map.put("pre_storage_deposit", oceanPreConfiguration.getPreStorageDeposit());
		map.put("cus_plan_deposit_path", oceanPreConfiguration.getCusPlanDepositPath());
		map.put("pre_storage_deposit_path", oceanPreConfiguration.getPreStorageDepositPath());
    map.put("customer_id", oceanPreConfiguration.getCustomerId()==null?null:oceanPreConfiguration.getCustomerId()+"");
    map.put("customer", oceanPreConfiguration.getCustomer());
    map.put("box_type", oceanPreConfiguration.getBoxType());
		map.put("cus_plan_price", oceanPreConfiguration.getCusPlanPrice());
		map.put("pre_storage_price", oceanPreConfiguration.getPreStoragePrice());
    map.put("pre_date", Utils.isNotEmpty(oceanPreConfiguration.getPreDate())? oceanPreConfiguration.getPreDate(): DateUtil.getCurrentDate());
		map.put("update_user", loginUser.getUserId() + "");
		map.put("id", oceanPreConfiguration.getId());
		getMapParameter(sql, map);
		int count = jdbcTemplate.update(sql.toString(), map);
		return count > 0;
	}

	@Override
	public boolean remove(OceanPreConfiguration oceanPreConfiguration, User loginUser) {
		int count = 0;
		Map<String, String> map = null;
		if(oceanPreConfiguration.getId().length()>0){
			for(String ID : oceanPreConfiguration.getId().split(",")){
				map  = new HashMap<String, String>();
				if(ID==null || ID.equals("")){ break; }
				StringBuffer sql =new StringBuffer("update chf_ocean_pre_configuration set deleted=1" +
						",update_user=:update_user, update_time=now() "
						+ " where id =:id ");
				map.put("update_user", loginUser.getUserId() + "");
				map.put("id", ID);
				getMapParameter(sql, map);
				count = jdbcTemplate.update(sql.toString(),map);
			}
		}
		return count > 0;
	}
}
